﻿Imports System.Data.OleDb
Imports Subgurim.Controles

Partial Public Class Update
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If String.IsNullOrEmpty(Session("RESTID")) Then
            Response.Redirect("List.aspx")
        End If


        If Not Page.IsPostBack Then

            Checkuser()

            GMap1.setCenter(New GLatLng(24.69943, 46.72348))
            Dim options As New GMapUIOptions()
            options.maptypes_physical = False
            options.zoom_scrollwheel = True
            GMap1.addGMapUI(New GMapUI())
            GMap1.GZoom = 10

            Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("RUHRestaurantsDB").ConnectionString)


            Dim RESTID As Integer = Session("RESTID")
            'Label1.Text = RESTID

            ' list images was uploaded
            SqlDataSource1.SelectCommand = "SELECT [RestId], [ImgName] FROM [Rest_Images] WHERE RestId=" & RESTID & ""

            Dim restname As String = "Select id,RestName,Address,Telephone,Email,WorkingHours,PriceLevel,LuxuryLevel,EatingService from Resturants WHERE id=" & RESTID & ""
            Dim SQL As OleDbCommand = New OleDbCommand(restname, OleDbConn)
            SQL.CommandType = CommandType.Text
            OleDbConn.Open()
            Dim RestaurantName = SQL.ExecuteReader()

            If RestaurantName.HasRows Then

                If RestaurantName.Read() Then
                    Dim addrs As String = RestaurantName.Item("Address")
                    addrs = (addrs.Replace("(", " ")).Replace(")", " ").Trim()
                    Dim address As String() = addrs.Split(",")

                    Dim center As New GLatLng(address(0), address(1))

                    GMap1.setCenter(center, 14)
                    Dim icon As New GMarker(center)
                    Dim window As New GInfoWindow(icon, "<br>" & RestaurantName.Item("RestName") & "</b>", False, GListener.Event.click)
                    GMap1.addInfoWindow(window)


                    RestNametxt.Text = RestaurantName.Item("RestName").ToString
                    Addresstxt.Text = RestaurantName.Item("Address").ToString

                    Telephonetxt.Text = RestaurantName.Item("Telephone").ToString
                    Emailtxt.Text = RestaurantName.Item("Email").ToString
                    Hourstxt.Text = RestaurantName.Item("WorkingHours").ToString

                    For Each itms As ListItem In DropDownList1.Items
                        If itms.Value = RestaurantName.Item("PriceLevel").ToString Then
                            itms.Selected = True
                        End If
                    Next

                    For Each itms As ListItem In DropDownList2.Items
                        If itms.Value = RestaurantName.Item("LuxuryLevel").ToString Then
                            itms.Selected = True
                        End If
                    Next

                    For Each itms As ListItem In DropDownList3.Items
                        If itms.Value = RestaurantName.Item("EatingService").ToString Then
                            itms.Selected = True
                        End If
                    Next

                End If


                RestaurantName.Close()
            End If

            Dim Facilities As String = "Select Internet,Parking,Delivery,Toilet,ChildChair,TV,Music,PlayingArea from Facilities WHERE Restid=" & RESTID & ""
            Dim FSQL As OleDbCommand = New OleDbCommand(Facilities, OleDbConn)
            FSQL.CommandType = CommandType.Text


            Dim Fcheckbox = FSQL.ExecuteReader

            If Fcheckbox.HasRows() Then

                While Fcheckbox.Read()

                    If Fcheckbox.Item("Internet") = True Then
                        CheckBox11.Checked = True
                    Else
                        CheckBox11.Checked = False

                    End If
                    If Fcheckbox.Item("Parking") = True Then
                        CheckBox12.Checked = True
                    Else
                        CheckBox12.Checked = False

                    End If
                    If Fcheckbox.Item("Delivery") = True Then
                        CheckBox13.Checked = True
                    Else
                        CheckBox13.Checked = False

                    End If
                    If Fcheckbox.Item("Toilet") = True Then
                        CheckBox14.Checked = True
                    Else
                        CheckBox14.Checked = False

                    End If
                    If Fcheckbox.Item("ChildChair") = True Then
                        CheckBox15.Checked = True
                    Else
                        CheckBox15.Checked = False

                    End If
                    If Fcheckbox.Item("TV") = True Then
                        CheckBox16.Checked = True
                    Else
                        CheckBox16.Checked = False

                    End If
                    If Fcheckbox.Item("Music") = True Then
                        CheckBox17.Checked = True
                    Else
                        CheckBox17.Checked = False

                    End If
                    If Fcheckbox.Item("PlayingArea") = True Then
                        CheckBox18.Checked = True
                    Else
                        CheckBox18.Checked = False

                    End If

                End While
                Fcheckbox.Close()

            End If


            Dim FoodType As String = "Select FoodTypeId from Rest_Food_Type Where Restid =" & RESTID & ""
            Dim FoodSQL As OleDbCommand = New OleDbCommand(FoodType, OleDbConn)
            FoodSQL.CommandType = CommandType.Text

            Dim FoodList = FoodSQL.ExecuteReader

            If FoodList.HasRows Then
                ListBox1.DataBind()

                While FoodList.Read()
                    Dim F = FoodList.Item("FoodTypeId")

                    For i = 0 To ListBox1.Items.Count - 1

                        For Each x As ListItem In ListBox1.Items

                            If (ListBox1.Items.FindByValue(F).Value) Then

                                ListBox1.Items.FindByValue(F).Selected = True

                            Else

                                RequiredFieldValidator7.Enabled = False
                            End If
                        Next
                    Next

                End While

                FoodList.Close()
            End If


            OleDbConn.Close()

        End If

      


    End Sub



    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click


        Dim RESTID = Session("RESTID")
     
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("RUHRestaurantsDB").ConnectionString)

        Dim RestaurantsInsert As String = "UPDATE Resturants SET [RestName]=@RestName,[Address]=@Address,[Telephone]=@Telephone,[Email]=@Email,[WorkingHours]=@WorkingHours, [PriceLevel]=@PriceLevel, [LuxuryLevel]=@LuxuryLevel, [EatingService]=@EatingService,[UserName]=@UserName WHERE Id=" + RESTID + ""

        Dim SQLcmd = New OleDbCommand(RestaurantsInsert, OleDbConn)

        Dim FacilitatsInsert As String = "UPDATE Facilities SET Restid=@Restid,Internet=@Internet,Parking=@Parking,Delivery=@Delivery,Toilet=@Toilet,ChildChair=@ChildChair,TV=@TV,Music=@Music,PlayingArea=@PlayingArea WHERE Restid=" & RESTID & ""
        Dim SQL2cmd = New OleDbCommand(FacilitatsInsert, OleDbConn)

        SQLcmd.CommandType = CommandType.Text
        SQL2cmd.CommandType = CommandType.Text

        'Resturants table

        SQLcmd.Parameters.AddWithValue("@RestName", RestNametxt.Text)
        SQLcmd.Parameters.AddWithValue("@Address", Addresstxt.Text)
        SQLcmd.Parameters.AddWithValue("@Telephone", Telephonetxt.Text)
        SQLcmd.Parameters.AddWithValue("@Email", Emailtxt.Text)
        SQLcmd.Parameters.AddWithValue("@WorkingHours", Hourstxt.Text)
        SQLcmd.Parameters.AddWithValue("@PriceLevel", DropDownList1.SelectedItem.Text)
        SQLcmd.Parameters.AddWithValue("@LuxuryLevel", DropDownList2.SelectedItem.Text)
        SQLcmd.Parameters.AddWithValue("@EatingService", DropDownList3.SelectedItem.Text)
        SQLcmd.Parameters.AddWithValue("@UserName", User.Identity.Name)

        OleDbConn.Open()
        SQLcmd.ExecuteNonQuery()
        SQLcmd.Dispose()

        SQL2cmd.Parameters.AddWithValue("@Restid", RESTID)

        If CheckBox11.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Internet", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Internet", "0")
        End If

        If CheckBox12.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Parking", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Parking", "0")
        End If
        If CheckBox13.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Delivery", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Delivery", "0")
        End If
        If CheckBox14.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Toilet", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Toilet", "0")
        End If
        If CheckBox15.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@ChildChair", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@ChildChair", "0")
        End If
        If CheckBox16.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@TV", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@TV", "0")
        End If
        If CheckBox17.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Music", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Music", "0")
        End If
        If CheckBox18.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@PlayingArea", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@PlayingArea", "0")
        End If
        SQL2cmd.ExecuteNonQuery()


        Dim food As String = "SELECT FoodTypeId FROM Rest_Food_Type WHERE RestId=" & RESTID & ""
        Dim foodsql As OleDbCommand = New OleDbCommand(food, OleDbConn)
        foodsql.CommandType = CommandType.Text
        Dim foodlist = foodsql.ExecuteReader

        Dim FoodListArry As New ArrayList

        If foodlist.HasRows Then
            While foodlist.Read
                FoodListArry.Add(foodlist.Item("FoodTypeId"))
            End While
        End If

        Dim W As Integer
        For Each W In FoodListArry

            Dim deleteItems As String = "Delete FROM [Rest_Food_Type] WHERE RestId=" & RESTID & " And FoodTypeId=" & W & ""
            Dim DelSQL As OleDbCommand = New OleDbCommand(deleteItems, OleDbConn)
            DelSQL.CommandType = CommandType.Text
            DelSQL.ExecuteNonQuery()

        Next

     

        For Each itms As ListItem In ListBox1.Items

            If itms.Selected = True Then
                Dim RestFoodTypeInsert As String = "INSERT INTO Rest_Food_Type(RestId,FoodTypeId) VALUES(@RestId,@FoodTypeId)"
                Dim SQL4cmd As OleDbCommand = New OleDbCommand(RestFoodTypeInsert, OleDbConn)
                SQL4cmd.CommandType = CommandType.Text

                SQL4cmd.Parameters.AddWithValue("@Restid", RESTID)
                SQL4cmd.Parameters.AddWithValue("@FoodTypeId", itms.Value)
                SQL4cmd.ExecuteNonQuery()
            End If

        Next



        Dim fID = Session("RESTID")

        'Upload images Rest_Images table.
        If (FileUpload1.HasFile = True) And (FileUpload1.FileBytes.Length <= 845941) Then

            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload1.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload1.FileName)
            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload1.FileName)
            SQLimg.ExecuteNonQuery()

        End If


        If (FileUpload2.HasFile = True) And (FileUpload2.FileBytes.Length <= 845941) Then
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload2.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload2.FileName)

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload2.FileName)
            SQLimg.ExecuteNonQuery()


        End If

        If (FileUpload3.HasFile = True) And (FileUpload3.FileBytes.Length <= 845941) Then
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload3.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload3.FileName)

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload3.FileName)
            SQLimg.ExecuteNonQuery()


        End If

        If (FileUpload4.HasFile = True) And (FileUpload4.FileBytes.Length <= 845941) Then
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload4.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload4.FileName)

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload4.FileName)
            SQLimg.ExecuteNonQuery()


        End If
        OleDbConn.Dispose()
        OleDbConn.Close()

        Response.Redirect("List.aspx")

    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        Response.Redirect("List.aspx")

    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
        Dim RESTID As Integer = Session("RESTID")

        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("RUHRestaurantsDB").ConnectionString)
        OleDbConn.Open()

        Dim imgs As String = "Delete FROM [Rest_Images] WHERE RestId=" & RESTID & ""
        Dim IMGSQL As OleDbCommand = New OleDbCommand(imgs, OleDbConn)
        IMGSQL.CommandType = CommandType.Text
        IMGSQL.ExecuteNonQuery()
        OleDbConn.Close()

    End Sub

    Sub Checkuser()

        If (User.IsInRole("administrators")) Then

            Return
        Else


            Dim RESTID = Session("RESTID")

            Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("RUHRestaurantsDB").ConnectionString)

            Dim Username As String = "Select UserName from Resturants WHERE Id=" & RESTID & ""
            Dim SQL As OleDbCommand = New OleDbCommand(Username, OleDbConn)
            SQL.CommandType = CommandType.Text
            OleDbConn.Open()

            Dim UserID = SQL.ExecuteReader()

            If UserID.HasRows Then

                While UserID.Read()
                    If Not (UserID.Item("Username") = User.Identity.Name) Then

                        MsgBox("You cannot update this page", MsgBoxStyle.Critical, "Authentication")

                        Response.Redirect("List.aspx")
                    End If


                End While

            End If
            UserID.Close()
            OleDbConn.Close()
        End If
    End Sub

    Protected Function GMap1_Click(ByVal s As Object, ByVal e As GAjaxServerEventArgs) As String
        Return New GMarker(e.point).ToString(e.map)

    End Function

    Protected Function GMap1_MarkerClick(ByVal s As Object, ByVal e As GAjaxServerEventArgs) As String
        Dim markerID As String = GMap1.getGMapElementById(e.who)

        Dim js As String = String.Format(vbCr & vbLf & "if ({0}.isHidden())" & vbCr & vbLf & " {0}.show()" & vbCr & vbLf & " else" & vbCr & vbLf & " {0}.hide();" & vbCr & vbLf & "  ", markerID)

        Dim Address = markerID

        js += String.Format("javascript:AddAddress( {0}.getPoint());", Address)

        Return js
    End Function
End Class